package cn.com.customer.manager.ui.message.email

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

import cn.com.customer.manager.R
import cn.com.customer.manager.adapter.EmailAdapter
import cn.com.customer.manager.base.BaseFragment
import cn.com.customer.manager.bean.EmailInfo
import cn.com.customer.manager.bean.EmailListInfo
import com.chad.library.adapter.base.BaseQuickAdapter
import kotlinx.android.synthetic.main.fragment_email.*

/**
 * A simple [Fragment] subclass.
 */
class EmailFragment : BaseFragment(), BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener, EmailConstract.View {
    private lateinit var mAdapter: EmailAdapter
    private var page: Int = 1
    private var mPresenter: EmailConstract.Presenter = EmailPresenter(this)
    override fun getLayoutId(): Int = R.layout.fragment_email
    lateinit var list: MutableList<EmailInfo>
    override fun initView() {
        initRv()
        requestData()
    }

    private fun initRv() {
        email_Recycler.layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
        mAdapter = EmailAdapter(R.layout.item_email_list)
        email_Recycler.adapter = mAdapter
        mAdapter.onItemClickListener = this
        mAdapter.onItemChildClickListener = this

    }

    private fun requestData() {
        var params: MutableMap<String, String> = mutableMapOf()
        params["data"] = ""
        mPresenter.getEmailList(params)
    }


    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        startActivity(Intent(activity, EmailDetailActivity::class.java).putExtra("data",list.get(position).mailId))
    }

    companion object {

        @JvmStatic
        fun newInstance() = EmailFragment().apply {
            arguments = Bundle().apply {

            }
        }
    }


    override fun setEmailList(result: EmailListInfo) {
        list = result.notReadList
        mAdapter.setNewData(list)

    }

    override fun setPresenter(presenter: EmailConstract.Presenter) {
        mPresenter = presenter
    }
}
